package com.examination.controller.integralModel;


import com.examination.entity.Commodity;
import com.examination.service.integralModel.CommodityService;
import com.examination.util.JsonResult;
import com.examination.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Api(description = "商品接口")
@RestController
@RequestMapping("commodity")
@CrossOrigin
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @ApiOperation(value = "增加商品" , notes = "")
    @PostMapping("insertCommodity")
    public JsonResult insertCommodity(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String time = format.format(now);
        map.put("upTime", time);
        int i = commodityService.insertCommodity(map);
        if(i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("增加成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("增加失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "删除商品" , notes = "")
    @PostMapping("deleteCommodity")
    public JsonResult deleteCommodity(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = commodityService.deleteCommodity(map);
        if(i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "修改商品" , notes = "")
    @PostMapping("updateCommodity")
    public JsonResult updateCommodity(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = commodityService.updateCommodity(map);
        if(i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("修改成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "分页模糊查询商品信息" , notes = "")
    @PostMapping("/findCommodity")
    public Page<Commodity> findCommodity(@RequestBody Map map) {
        Page<Commodity> page = new Page<Commodity>();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(commodityService.Total(map));
        page.setItems(commodityService.findCommodity(map));
        return page;
    }

    @ApiOperation(value = "下拉框查询商品信息" , notes = "")
    @PostMapping("/selectCommodity")
    public List<Commodity> selectCommodity(@RequestBody Map map) {
        return commodityService.selectCommodity(map);
    }
}
